package messages;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.XmlSerializationUtils;

public abstract class AutoGenericMessage extends GenericMessage implements Convertible {
    
    private static final long serialVersionUID = 1L;


    public AutoGenericMessage(String s, String sign, Integer num) {
        super();
        _objectType = this.getClass().getSimpleName();
        _objectString = s;
        _signature = sign;
        _number = num;
    }
    
    /**
     * The Default Constructor
     * 
     * NOTE: ALL SUBCLASSES MUST IMPLEMENT THE DEFAULT CONTRUCTOR.
     */
    
    public AutoGenericMessage() {
        
    } 
    
    @Override
    public Element convertToCanonic(Document doc) {
        Element el = doc.createElement(_objectType);

        if (_objectType != null) {
            Element childEl = doc.createElement("ObjectType");
            childEl.setTextContent(_objectType);
            el.appendChild(childEl);
        } 
        
        if (_objectString != null) {
            Element childEl = doc.createElement("ObjectString");
            childEl.setTextContent(_objectString);
            el.appendChild(childEl);

        } 

        if (_signature != null) {
            Element childEl = doc.createElement("Signature");
            childEl.setTextContent(_signature);
            el.appendChild(childEl);

        } 

        Element childEl = doc.createElement("Number");
        childEl.setTextContent(_number.toString());
        el.appendChild(childEl);

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._objectType = XmlSerializationUtils.getTextValue(el, "ObjectType");
        this._objectString = XmlSerializationUtils.getTextValue(el, "ObjectString");
        this._signature = XmlSerializationUtils.getTextValue(el, "Signature");
        this._number = XmlSerializationUtils.getIntValue(el, "Number");
        return this;
    }
}
